<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>chunkify</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const chunkify = function* (itr, size) {
            const chunk = [];
            for (const item of itr) {
                chunk.push(item);
                if (chunk.length === size) {
                    yield chunk;
                    chunk = [];
                }
            }
            if (chunk.length) {
                yield chunk;
            }
        };
        const x = new Set([1, 2, 1, 3, 4, 1, 2, 5]);
        console.log([...chunkify(x, 2)]);; // [[1, 2], [3, 4], [5]]
    </script>
</body>

</html>